﻿package frEngine.loaders.away3dMd5
{
    import __AS3__.vec.*;
    import flash.geom.*;
    import frEngine.loaders.away3dMd5.md5Data.*;

    public class SkeletonPose extends Object
    {
        private var _joinPoseData:Vector.<Vector.<Number>>;
        public var numJointPoses:uint;
        private var frameData:FrameData;
        private var _parser:MD5AnimParserBase;
        private var _id:String;
        private static var _localPoses1:Vector.<JMatrix3D> = new Vector.<JMatrix3D>;
        private static var _localPoses2:Vector.<JMatrix3D> = new Vector.<JMatrix3D>;
        private static var id:uint = 0;

        public function SkeletonPose(param1:MD5AnimParserBase, param2:FrameData = null, param3:uint = 0)
        {
            this._joinPoseData = new Vector.<Vector.<Number>>;
            this._parser = param1;
            this.frameData = param2;
            this.numJointPoses = param3;
            this._joinPoseData.length = this.numJointPoses;
            this._id = ":" + id;
            var _loc_6:* = id + 1;
            id = _loc_6;
            var _loc_4:* = _localPoses2.length;
            while (_loc_4 < this.numJointPoses)
            {
                
                _localPoses2.push(new JMatrix3D());
                _loc_4++;
            }
            _loc_4 = _localPoses1.length;
            while (_loc_4 < this.numJointPoses)
            {
                
                _localPoses1.push(new JMatrix3D());
                _loc_4++;
            }
            return;
        }// end function

        private function translatePose(param1:int) : Vector.<Number>
        {
            var _loc_7:int = 0;
            var _loc_8:JMatrix3D = null;
            var _loc_9:Vector.<Number> = null;
            if (this._joinPoseData[param1])
            {
                throw new Error("请检查translatePose是否存在");
            }
            var _loc_2:* = this._parser.hierarchy;
            var _loc_3:* = this._parser.baseFrameData;
            if (_loc_2 == null || _loc_3 == null)
            {
                return null;
            }
            var _loc_4:* = _loc_2[param1];
            var _loc_5:* = _localPoses2[param1];
            if (_loc_5.userId != this._id)
            {
                _loc_5.parser(_loc_4, this.frameData.components, _loc_3[param1]);
                _loc_5.userId = this._id;
                _loc_7 = _loc_4.parentIndex;
                if (_loc_7 > -1)
                {
                    _loc_9 = this._joinPoseData[_loc_7];
                    if (!_loc_9)
                    {
                        this.translatePose(_loc_7);
                        _loc_8 = _localPoses2[_loc_7];
                        _loc_5.append(_loc_8);
                    }
                    else
                    {
                        _loc_8 = _localPoses2[_loc_7];
                        _loc_8.userId = this._id;
                        _loc_8.copyRawDataFrom(_loc_9);
                        _loc_5.append(_loc_8);
                    }
                }
            }
            var _loc_6:* = _loc_5.rawData;
            this._joinPoseData[param1] = _loc_6;
            return _loc_6;
        }// end function

        public function getGlobalProperties(param1:Skeleton, param2:Vector.<int>) : Vector.<Number>
        {
            var _loc_5:Matrix3D = null;
            var _loc_6:JMatrix3D = null;
            var _loc_10:Vector.<Number> = null;
            var _loc_11:int = 0;
            var _loc_3:* = param1.meshUrl + this._id;
            var _loc_4:* = param1.inherits;
            var _loc_7:* = param2.length;
            var _loc_8:int = 0;
            var _loc_9:* = new Vector.<Number>;
            while (_loc_8 < _loc_7)
            {
                
                _loc_11 = param2[_loc_8];
                _loc_6 = _localPoses1[_loc_11];
                if (_loc_6.userId != _loc_3)
                {
                    _loc_10 = this._joinPoseData[_loc_11];
                    if (_loc_10 == null)
                    {
                        _loc_10 = this.translatePose(_loc_11);
                    }
                    _loc_5 = _loc_4[_loc_11].inverseTransform;
                    _loc_6.copyRawDataFrom(_loc_10);
                    _loc_6.prepend(_loc_5);
                    _loc_6.userId = _loc_3;
                }
                _loc_6.copyRawDataTo(_loc_9, _loc_8 * 12, true);
                _loc_8++;
            }
            return _loc_9;
        }// end function

        public function jointPoseFromIndex(param1:int, param2:Matrix3D, param3:Vector3D) : void
        {
            var _loc_4:* = this._joinPoseData[param1];
            if (_loc_4 == null)
            {
                _loc_4 = this.translatePose(param1);
            }
            if (param2 != null)
            {
                param2.copyRawDataFrom(_loc_4);
            }
            if (param3)
            {
                param3.x = _loc_4[12];
                param3.y = _loc_4[13];
                param3.z = _loc_4[14];
            }
            return;
        }// end function

        public function dispose() : void
        {
            this._joinPoseData.length = 0;
            this._parser = null;
            return;
        }// end function

    }
}
